#include "fun.h"

u8 bdata com;
u8 disp_flag = 0,param = 27;
bit m_s;
bit c_flag;
bit relay_flag;
sbit BUZZER = com^6;
sbit RELAY = com^4;

void buzzer(u8 cmd)
{
	BUZZER = cmd;
	P0 = com;
	P2 = 0xa0;
	_nop_();
	P2 = 0x00;
}

void relay(u8 cmd)
{
	RELAY = cmd;
	P0 = com;
	P2 = 0xa0;
	_nop_();
	P2 = 0x00;
}

void led(u8 ucode)
{
	P0 = ~ucode;
	P2 = 0x80;
	_nop_();
	P2 = 0x00;
}



void key_scan(void)
{
	static u8 key_up = 1;
	u16 k_status;
	P42 = 0;P44 = 1;
	P35 = 1;P34 = 1;
	k_status = P3 & 0x0f;
	P42 = 1;P44 = 0;
	k_status = (k_status << 4) | (P3 & 0x0f);
	P44 = 0;P35 = 0;
	k_status = (k_status << 4) | (P3 & 0x0f);
	P35 = 1;P34 = 0;
	k_status = (k_status << 4) | (P3 & 0x0f);
	k_status = ~k_status;
	k_status &= 0x00cc;
	
	if(key_up && k_status)
	{
		key_up = 0;
		if(k_status == 0x0080)
		{
			disp_flag++;
			if(disp_flag == 3)
				disp_flag = 0;
		}
		else if(k_status == 0x0040)
		{
			c_flag ^= 1;
			relay_flag = 0;
		} 
		else if(k_status == 0x0008 && disp_flag == 2 && param < 99)
			param += 1;
		else if(k_status == 0x0004 && disp_flag == 2 && param > 0)
			param -= 1;
		else if(k_status == 0x0004 && disp_flag == 1)
			m_s = 1;
	}
	else if(!k_status)
		key_up = 1;
	
	if(key_up && m_s)
		m_s = 0;
}

void smg_disp(u8 num,u8 position)
{
	P0 = 0xff;
	P2 = 0xe0;
	_nop_();
	P2 = 0x00;
	
	P0 = position;
	P2 = 0xc0;
	_nop_();
	P2 = 0x00;
	
	P0 = ~num;
	P2 = 0xe0;
	_nop_();
	P2 = 0x00;
}




